package org.snail;

import com.google.common.collect.Maps;
import org.snail.core.ServiceMetadata;
import org.snail.transport.Acceptor;
import org.snail.transport.AcceptorDispatcher;
import org.snail.transport.impl.NettyAcceptor;

import java.util.Map;

/**
 * 一个应用只有个server
 * <p>
 * Created by fish on 17/9/9.
 */
public class SnailServer {

    private SnailServer() {
    }

    public static SnailServer getInstance() {
        return SnailServer.ClientHolder.instance;
    }

    private static class ClientHolder {
        private final static SnailServer instance = new SnailServer();
    }

    private AcceptorDispatcher dispatcher;

    private Acceptor acceptor;


    /**
     * 临时测试
     */
    private Map<ServiceMetadata,Object> serviceClazz = Maps.newConcurrentMap();


    public SnailServer newDispatcherInstance() {
        if (dispatcher == null) {
            this.dispatcher = new AcceptorDispatcher();
        }
        return this;
    }

    public SnailServer newAcceptorInstance(int port) {
        if (acceptor == null) {
            this.acceptor = new NettyAcceptor();
            this.acceptor.bindDispatcher(dispatcher);
            this.acceptor.start(port);
        }
        return this;
    }

    public SnailServer addServiceClazz(ServiceMetadata metadata,Object service){
        this.serviceClazz.put(metadata,service);
        return this;
    }

    public Object getServiceClazz(ServiceMetadata metadata){
        return this.serviceClazz.get(metadata);
    }
}
